<?php
namespace app\common\utils;

use app\admin\model\LeescoreOrder;
use app\common\model\shop\Order;
use app\common\model\User;

/**
 * 公用方法服务
 * fast专用统计模块
*/

class Statistics {
    /**
     * 获取echart数据
     * @param $time_type
     */
    public static function get($time_type = 0, $create_time = '') {
        $timestamp = time();
        $date_type = 'days';
        $style = 'Y-m-d';
        $length = 12;
        $type_zh = '';
        switch($time_type){
            case 0 :
                $start_time_today = mktime(0,0,0,1,1, date('Y'));
                $end_time_today = strtotime(date('Y-12-31 23:59:59'));
                $where_total_task['createtime'] = array('between', array($start_time_today, $end_time_today));
                $date_type = 'months';
                $style="Y-m";
                $length = 12;
                $type_zh = '本年';
                break;
            case 1 :
                //今天
                $start_time_today = strtotime(date('Y-m-d'));
                $end_time_today = strtotime(date('Y-m-d').'+1 day');
                $where_total_task['createtime'] = array('between', array($start_time_today, $end_time_today));
                $date_type = 'hours';
                $style = 'H:00:00';
                $length = 24;
                $type_zh = '今日';

                break;
            case 2 :
                //本周
                $start_time_weekly = strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp)));
                $end_time_weekly = strtotime(date('Y-m-d', strtotime("next week Monday", $timestamp)))  - 1;
                $where_total_task['createtime'] = array('between', array($start_time_weekly, $end_time_weekly));
                $length = 7;
                $type_zh = '本周';
                break;
            case 3 :
                //本月
                $start_time_month = mktime(0, 0, 0, date('m'), 1, date('Y'));
                $end_time_month = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
                $where_total_task['createtime'] = array('between', array($start_time_month, $end_time_month));
                $length = date('t');
                $style="m-d";
                $type_zh = '本月';

                break;
            case 4 :
                //本季度
                $season = ceil((date('n'))/3);//当月是第几季度
                $start_time_jidu = mktime(0, 0, 0,$season*3-3+1,1,date('Y'));
                $end_time_jidu = mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'));
                $where_total_task['createtime'] = array('between', array($start_time_jidu, $end_time_jidu));
                $date_type = 'months';
                $length = 7;
                $style="Y-m";
                $type_zh = '本季度';

                break;
            case 5 :
                //本年
                $start_time_year = mktime(0,0,0,1,1,date('Y', $timestamp));
                $end_time_year = mktime(23,59,59,12,31,date('Y', $timestamp));
                $where_total_task['createtime'] = array('between', array($start_time_year, $end_time_year));
                $date_type = 'months';
                $style = "Y-m";
                $type_zh = '本年';

                break;
            case 6 :
                $time_array = explode(' - ', $create_time);
                $start_time = strtotime($time_array[0]);
                $end_time = strtotime($time_array[1]);
                $where_total_task['createtime'] = array('between', array($start_time, $end_time));
                $data = Utils::getLengthAndFormat(($end_time - $start_time));
                $date_type = $data['date_type'];
                $length = $data['length'];
                $style = $data['style'];
                break;
            default : ;break;
        }

        return [
            'date_type' => $date_type,
            'style' => $style,
            'length' => $length,
            'time' => $where_total_task,
            'type_zh'  => $type_zh
        ];
    }

    /**
     * 图表统计数据
     * @param array $where_data 筛选条件
     * @param string $date_type 时间间隔区间   hours   days  months
     * @param string $show_style  时间展示样式
     * @param int $date_lenght  时间间隔长度
     * @return array
     * @throws Exception
     */
    public static function getDashboard($where = [],  $date_type = 'days', $show_style = 'Y-m-d', $date_lenght = 12){
        $end_time = time();
        if(isset($where['createtime'])){
            $end_time =  $where['createtime'][1][1]+1; //时间区间 结束时间  筛选时 设置 当天23：59：59， 形成图表时，取日期有误
        }
        //echarts 流水，任务统计
        $usersStatics = [];
        $vistStatics = [];
        $niuStatics = [];
        $giftStatics = [];

        for($i = 1; $i <= $date_lenght; $i++) {
            if ($i == 1) {
                $start = strtotime ((-$i). ' '. $date_type, $end_time);
                $end = $end_time;
            } else {
                $start = strtotime ((-$i). ' '. $date_type, $end_time);
                $end = strtotime ((-$i+1). ' '. $date_type, $end_time);
            }
            $startTime = $start;
            $endTime = $end;
            $startFormat = Utils::time_format($startTime,  $show_style);
            $where['createtime'] = ['between', [$startTime, $endTime]];

            $usersStatics[$startFormat] = User::where($where)->where(['status'=> 'normal'])->count();
            $vistStatics[$startFormat] = User::where(['logintime' => ['between', [$startTime, $endTime]]])->where(['status'=> 'normal'])->count();
            $niuStatics[$startFormat] = Order::where($where)->count();
            $giftStatics[$startFormat] = LeescoreOrder::where($where)->count();
        }

        return [
            "column" => array_keys($usersStatics),
            'usersStatics' => array_values($usersStatics),
            'vistStatics' => array_values($vistStatics),
            'niuStatics' => array_values($niuStatics),
            'giftStatics' => array_values($giftStatics),
        ];
    }

    public static function rightData($where) {
        //今日注册
        $data['register'] = User::where(['jointime'=> $where['createtime']])->count();
        $data['login'] = User::where(['logintime' => $where['createtime']])->count(); //登陆
        $data['niu'] = Order::where($where)->count();  //牛订单
        $data['niuno'] = Order::where($where)->where(['type' => 0])->count();  //牛订单
        $data['gift'] = LeescoreOrder::where($where)->count();  //积分商城订单
        $data['giftno'] = LeescoreOrder::where($where)->where(['status' => 1])->count();  //积分商城订单

        return $data;
    }

}
